<?php


namespace App\Http\Requests\Admin\Business;

use App\Exceptions\SysException;
use App\Http\Requests\BaseFormRequest;
use App\Utils\Verify;

class SubAccountAgentSaveFormRequest extends BaseFormRequest
{

    protected function passedValidation()
    {
        $ratio = $this->get('ratio', '1');
        if (Verify::money($ratio, 4, 2) === false) {
            throw new SysException('分成比例格式错误');
        }
        if ($ratio > 20) {
            throw new SysException('分成比例不能超过20%');
        }
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $id = $this->get('id', 0);
        $roles = [
            'ratio' => [
                'required',
            ]
        ];
        if (!$id) {
            $roles['sub_account'] = [
                'required'
            ];
        }
        return $roles;
    }
}